package de.sp.games.snake12k;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class GameOverState extends BasicGameState
{

	private int stateID = 2;
	private boolean newEntrance = true;

	private Image background;
	private Image gameOver;

	private UnicodeFont uF;
	private UnicodeFont uFBigger;
	private UnicodeFont uFSmaller;

	private int heightStartValue = 100;

	private ScoreList score = ScoreList.getInstance();
	private boolean isEntrySaved = false;
	private boolean soundPlayed = false;

	private Sound newHighScore;

	private TextField nameInput;
	private String playerName = "???";
	private boolean isStartTextDelated = false;

	public GameOverState(int stateID)
	{
		this.stateID = stateID;
	}

	public int getID()
	{
		return stateID;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
	{
		background = new Image("ressource/box_wall.jpg");
		gameOver = new Image("ressource/gameover.png");
		background.setFilter(Image.FILTER_NEAREST);

		newHighScore = new Sound("ressource/new_highscore.wav"); //FIXME: sound abspielen lassen wenn eine neue Highscore erreicht wurde

		uF = new UnicodeFont(new java.awt.Font("Verdana", java.awt.Font.BOLD, 36));
		uF.getEffects().add(new ColorEffect(java.awt.Color.ORANGE));
		uF.addAsciiGlyphs();
		uF.loadGlyphs();
		uFBigger = new UnicodeFont(new java.awt.Font("Verdana", java.awt.Font.BOLD, 50));
		uFBigger.getEffects().add(new ColorEffect(java.awt.Color.RED));
		uFBigger.addAsciiGlyphs();
		uFBigger.loadGlyphs();
		uFSmaller = new UnicodeFont(new java.awt.Font("Verdana", java.awt.Font.BOLD, 20));
		uFSmaller.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
		uFSmaller.addAsciiGlyphs();
		uFSmaller.loadGlyphs();

		String snakeLengthString = "SnakeLength - " + (SUtil.snakeLength + 1);
		uF.drawString((gc.getWidth() / 2) - (uF.getWidth(snakeLengthString) / 2), heightStartValue + 245, snakeLengthString);

		nameInput = new TextField(gc, uF, (gc.getWidth() - 300) / 2, 450, 300, 55, new ComponentListener()
		{
			public void componentActivated(AbstractComponent source)
			{
				System.out.println("Entered: " + nameInput.getText());
				playerName = nameInput.getText();
				nameInput.deactivate();
				nameInput.setConsumeEvents(false);
				nameInput.setAcceptingInput(false);
			}
		});
		nameInput.setAcceptingInput(true);
		nameInput.setMaxLength(10);
		nameInput.setConsumeEvents(true);
		nameInput.setText("Your Name!");
	}

	@Override
	public void render(GameContainer gc, StateBasedGame arg1, Graphics g) throws SlickException
	{
		/* Background */
		g.scale((1.0f / 16 * gc.getWidth()), (1.0f / 16) * (gc.getHeight() - 30));
		background.draw();
		g.resetTransform();

		float scaleText = 1.0f;
		float heightStartValue = gc.getHeight() - gc.getHeight() / 1.15f;

		/* Title */
		gameOver.draw((gc.getWidth() / 2) - ((gameOver.getWidth() * scaleText) / 2), heightStartValue, scaleText);

		/* Stats */
		if (score.isNewHighScore(SUtil.actualScore()) && SUtil.isNomralMode())
		{
			String newScoreString = "New ";
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(newScoreString) / 2), heightStartValue + 105, newScoreString);

			String scoreString = "Highscore! - " + SUtil.actualScore();
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(scoreString) / 2), heightStartValue + 155, scoreString);

			nameInput.render(gc, g);
		}
		else
		{
			String scoreString = "Score - " + SUtil.actualScore();
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(scoreString) / 2), heightStartValue + 155, scoreString);
			nameInput.deactivate();
			nameInput.setConsumeEvents(false);
			nameInput.setAcceptingInput(false);
		}

		String snakeLengthString = "SnakeLength - " + (SUtil.snakeLength + 1);
		uF.drawString((gc.getWidth() / 2) - (uF.getWidth(snakeLengthString) / 2), heightStartValue + 245, snakeLengthString);

		String timeString = "Time - " + SUtil.time;
		uF.drawString((gc.getWidth() / 2) - (uF.getWidth(timeString) / 2), heightStartValue + 245 + 55, timeString);

		/* Commands */
		String optionsString = "Press: 'R' for Restart - 'ESC' for Menu";
		uFSmaller.drawString((gc.getWidth() / 2) - (uFSmaller.getWidth(optionsString) / 2), gc.getHeight() - 29, optionsString);

	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int arg2) throws SlickException
	{
		Input input = gc.getInput();

		if (newEntrance)
		{
			nameInput.setConsumeEvents(true);
			nameInput.setAcceptingInput(true);
			newEntrance = false;
		}

		if (score.isNewHighScore(SUtil.actualScore()))
		{
			if (!soundPlayed)
			{
				soundPlayed = true;
				newHighScore.play();
			}

			if (!isEntrySaved && !nameInput.hasFocus() && isStartTextDelated && !nameInput.isAcceptingInput())
			{
				System.out.println("Save new Entry");
				score.makeNewEntry(playerName, SUtil.actualScore());
				isEntrySaved = true;
				input.clearKeyPressedRecord();
			}
		}

		if (nameInput.hasFocus() && !isStartTextDelated)
		{
			nameInput.setText("");
			isStartTextDelated = true;
		}

		if (input.isKeyPressed(Input.KEY_R) && !nameInput.isAcceptingInput())
		{
			sbg.enterState(Snake12k.GAMEPLAYSTATE);
			SUtil.snakeLength = SUtil.startSnakeLength;
			SUtil.resetPlayedTime();
			SUtil.stopPlayedTime();
			SUtil.resetTimeMoveCounter();
			SUtil.gameOver = false;
			SUtil.fieldResetWithNewField();
			isEntrySaved = false;
			nameInput.setAcceptingInput(true);
			nameInput.setConsumeEvents(true);
			newEntrance = true;
			input.clearKeyPressedRecord();
		}
		if (input.isKeyPressed(Input.KEY_ESCAPE))
		{
			sbg.enterState(Snake12k.TITLEMENUSTATE);
			SUtil.snakeLength = SUtil.startSnakeLength;
			SUtil.resetPlayedTime();
			SUtil.resetTimeMoveCounter();
			SUtil.gameOver = false;
			SUtil.fieldResetWithNewField();
			isEntrySaved = false;
			nameInput.setAcceptingInput(true);
			nameInput.setConsumeEvents(true);
			newEntrance = true;
			input.clearKeyPressedRecord();
		}
	}
}
